(function () {
    var pageWidth = 750
    var screenWidth = window.screen.width
    var scale = screenWidth / pageWidth
    if (screenWidth > 750) scale = 1
    var meta =
      '<meta name="viewport" content="width=750' +
      ', initial-scale=' +
      scale +
      ', minimum-scale=' +
      scale +
      ', maximum-scale=' +
      scale +
      ', user-scalable=no">'
    document.write(meta)
})()

$(function() {
  var page1 = $('.page1')
  var page2 = $('.page2')
  var phone = $('.phone')
  var valiCode = $('.valiCode')
  var getValid = $('.get-code')
  var btn = $('.btn')

  // 获取url参数
  var getParamsFromUrl = function(key) {
    try {
      var params = {}
      location.search.substring(1).split('&').forEach(function(item){
        params[item.split('=')[0]] = item.split('=')[1]
      })
      return params[key]
    } catch(err) {
      layer.alert('不合法的参数，请检查链接地址')
    }
  }

  // 验证手机号
  var validatePhone = function(phone) {
    return {
      status: /^1\d{10}$/.test(phone),
      msg: phone ? '手机号不能为空' : '手机号码格式不正确'
    }
  }

  // 倒计时
  var countdown = function() {
    var total = 60
    getValid.addClass('disabled')
    getValid.text(total + 's后重试')
    var timmer = setInterval(function() {
      if (total) {
        total --
        getValid.text(total + 's后重试')
      } else {
        clearInterval(timmer)
        getValid.text('获取验证码')
        getValid.removeClass('disabled')
      }
    }, 1000)
  }

  // 页面切换
  var pageIn = function() {
    page1.hide()
    page2.addClass('show')
  }

  // 记录分享人id
  //var shareMan =getParamsFromUrl('shareId')

  // 发送验证码
  getValid.on('click', function() {
    if($(this).hasClass('disabled')) return false
    var phoneNum = phone.val()
    var validated = validatePhone(phoneNum)
    if (validated.status) {
       $.post(path+'/share/getRegisterCode', {
         mobile: phoneNum
       }, function(res) {
         if(res.msg=="success"){
        	   layer.msg('验证码发送成功，请注意查收')
         }else{
        	 layer.msg(res.msg)
         }
     
        countdown()
       })
    } else {
      layer.msg(validated.msg)
    }
  })

  // 提交
  btn.on('click', function(){
    if($(this).hasClass('active')) return false
    var phoneNum = phone.val()
    var valiCodeNum = valiCode.val()
    var validated = validatePhone(phoneNum)
    
    if (!validated.status) {
      layer.msg(validated.msg)
      return false
    }
    if (!valiCodeNum) {
      layer.msg('请输入验证码')
      return false
    }
    $(this).addClass('active')
    $(this).text('正在提交...')
     $.post(path + '/share/save', {
       shareMan: shareMan,
      phoneNum: phoneNum,
       valiCodeNum: valiCodeNum
     }, function(res) {
    	 if(res.msg=="success"){
    		   layer.msg('提交成功', function() {
    		        pageIn()
    		      })
       }else{
      	 layer.msg(res.msg)
       }
   
     })
  })
})